using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GUI_Webpage_CompletOrderFood : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["OrderFoodCart"] == null)
            {
                ltrWarning.Text = "Sorry, your order food cart is empty";
            }
            else
            {
                DataTable dt = (DataTable)Session["OrderFoodCart"];
                dlDisplayCart.DataSource = dt;
                dlDisplayCart.DataBind();
                OrderFoodInfo orderFoodInfo = new OrderFoodInfo();
                orderFoodInfo.OrderID = Session["BookingID"].ToString();
            }
    }
    protected void btnComplete_Click(object sender, EventArgs e)
    {
        OrderFoodInfo orderFoodInfo = new OrderFoodInfo();
        OrderingFoodDetailsInfo ofdInfo = new OrderingFoodDetailsInfo();
        OrderFoodBll orderBll = new OrderFoodBll();
        OrderFoodDetailsBll orderDetailsBll = new OrderFoodDetailsBll();

        orderFoodInfo.OrderID = Session["BookingID"].ToString();
        orderFoodInfo.CustomerName = txtFullName.Text;
        orderFoodInfo.DeliverName = txtDeliverName.Text;
        orderFoodInfo.DeliverAddress = txtAdress.Text;
        orderFoodInfo.Email = txtEmail.Text;
        orderFoodInfo.Phone = txtPhone.Text;
        orderFoodInfo.AccNo = txtAccount.Text;
        orderFoodInfo.DateOfOrder = DateTime.Now;
        orderFoodInfo.TimeNeedDeliver = Request.QueryString.Get("timeSelect");
        orderFoodInfo.Description = txtComment.Text;
        orderFoodInfo.Status = false;

        orderBll.CreateOrderFood(orderFoodInfo);
        
        //Browse through each food in datalist
        for (int i = 0; i < dlDisplayCart.Items.Count; i++)
        {
            //get all variable to set to orderFoodDetails
            Label foodID = (Label)dlDisplayCart.Items[i].FindControl("lblID");
            ofdInfo.FoodID = foodID.Text;
            TextBox quantity = (TextBox)dlDisplayCart.Items[i].FindControl("txtQuantity");
            ofdInfo.Quantity = int.Parse(quantity.Text);
            ofdInfo.OrderID = orderFoodInfo.OrderID;
            ofdInfo.ResID = Request.QueryString.Get("resID");

            orderDetailsBll.CreateOrderFoodDetails(ofdInfo);  
        }
        CommonUse cmu = new CommonUse();
        if (cmu.SendMail(orderFoodInfo.Email, orderFoodInfo.OrderID))
        {
            ltrThankYou.Text = "You completed your order. Please check your email!";
        }
        else
        {
            ltrThankYou.Text = "Co loi";
        }
        Session["bookingID"]= null;
        Session["OrderFoodCart"] = null;
    }
}
